//
//  HostViewController.m
//  ICViewPager
//
//  Created by Ilter Cengiz on 28/08/2013.
//  Copyright (c) 2013 Ilter Cengiz. All rights reserved.
//

#import "HostViewController.h"
#import "SearchViewController.h"

#import "HealthNewsListViewController.h"
#import "HealthNewsClass.h"
#import "HealthNewsDetailViewController.h"
@interface HostViewController () <ViewPagerDataSource, ViewPagerDelegate>

@property (nonatomic,retain)NSMutableArray * newsArray;
@end

@implementation HostViewController

- (void)dealloc
{
    self.newsArray = nil;
    [super dealloc];
}
- (void)viewDidLoad {
    
    self.dataSource = self;
    self.delegate = self;
    
    self.navigationItem.title = @"资讯列表";
    
    // Keeps tab bar below navigation bar on iOS 7.0+
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    
    [self p_setupNewsClass];
    [super viewDidLoad];
    
    UIBarButtonItem * searchButtonTtem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self  action:@selector(didClickSearchButtonTtemAction:)];
    self.navigationItem.rightBarButtonItem = searchButtonTtem;
    [searchButtonTtem release];
    

}

- (void)didClickSearchButtonTtemAction:(UIBarButtonItem *)searchButton
{
    SearchViewController * searchVC = [[SearchViewController alloc]init];
    searchVC.type = newsType;
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];
}



#pragma mark------------------资讯分类---------------
//显示资讯分类scrollView
- (void)p_setupNewsClass
{
    //初始化可变数组
    self.newsArray = [NSMutableArray arrayWithCapacity:40];
    //资讯分类
    
    NSString * filePath = [[NSBundle mainBundle]pathForResource:@"HealthNewsClass.txt" ofType:nil];
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    
    NSArray *  sourceArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary * dic in sourceArray) {
        HealthNewsClass * newsCalss = [[HealthNewsClass alloc] init];
        [newsCalss setValuesForKeysWithDictionary:dic];
        [self.newsArray addObject:newsCalss];
        [newsCalss release];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ViewPagerDataSource
- (NSUInteger)numberOfTabsForViewPager:(ViewPagerController *)viewPager {
    return self.newsArray.count;
}
- (UIView *)viewPager:(ViewPagerController *)viewPager viewForTabAtIndex:(NSUInteger)index {
    
    HealthNewsClass * healthClass = self.newsArray[index];
    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:13.0];
    label.text = [NSString stringWithFormat:@"%@", healthClass.name];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    [label sizeToFit];
    
    return label;
}

- (UIViewController *)viewPager:(ViewPagerController *)viewPager contentViewControllerForTabAtIndex:(NSUInteger)index {
    
//    ContentViewController *cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"contentViewController"];
//    
//    cvc.labelString = [NSString stringWithFormat:@"Content View #%i", index];
//    
//    return cvc;
    
    HealthNewsListViewController * newsListVC = [[HealthNewsListViewController alloc]init];
    HealthNewsClass * healthClass = self.newsArray[index];
    newsListVC.newsClass = healthClass;
    __block HostViewController * hostVC = self;
    newsListVC.pushDVCBlock = ^(HealthNews * healthNews){
      
        HealthNewsDetailViewController * newsDetailVC = [[HealthNewsDetailViewController alloc] init];
        newsDetailVC.news = healthNews;
        [hostVC.navigationController pushViewController:newsDetailVC animated:YES];
        [newsDetailVC release];
        
    };
    return newsListVC;
}

#pragma mark - ViewPagerDelegate
- (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value {
    
    switch (option) {
        case ViewPagerOptionStartFromSecondTab:
            return 0.0;
            break;
        case ViewPagerOptionCenterCurrentTab:
            return 0.0;
            break;
        case ViewPagerOptionTabLocation:
            return 1.0;
            break;
        default:
            break;
    }
    
    return value;
}
- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color {
    
    switch (component) {
        case ViewPagerIndicator:
            return UIColorCommon;
            break;
        default:
            break;
    }
    
    return color;
}

@end
